package com.jd.charactercalculator.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;
import com.jd.charactercalculator.controller.common.BaseController;
import com.jd.charactercalculator.dto.param.UserParamDto;
import com.jd.charactercalculator.facade.UserFacade;
import com.jd.charactercalculator.util.LogUtil;
import weibo4j.Oauth;
import weibo4j.Users;
import weibo4j.Weibo;
import weibo4j.http.AccessToken;
import weibo4j.model.User;
import weibo4j.model.WeiboException;
import weibo4j.org.json.JSONException;



/**
 * @ClassName:OauthController
 * @Description: 
 * @author :孙林
 * @date :2012-8-27下午6:01:03
 */
public class OauthController extends BaseController {

	final Logger logger = LogUtil.getLogger();

	private String viewName;
	private UserFacade userFacade;

	public ModelAndView oauth(HttpServletRequest request, HttpServletResponse respnose) throws WeiboException, IOException, JSONException{
		
		//获取code
		String code = request.getParameter("code");

		//获取accessToken对象,并设置，accessToken
		Oauth oauth = new Oauth(); 
		AccessToken accessTokens = oauth.getAccessTokenByCode(code);
		String accessToken  = accessTokens.getAccessToken();
		Weibo weibo = new Weibo();
		weibo.setToken(accessToken);
		
		//获取accessToken，expireIn，refreshToken,uid
		String expireIn = accessTokens.getExpireIn();
		String refreshToken = accessTokens.getRefreshToken();
		String uid = accessTokens.getUid();

		//获取用户

		Users users = new Users();
		User user =  users.showUserById(uid);
		String sinaWeiboUsername  = user.getName();
		
		//得到用户名，code，accessToken从如数据库
		UserParamDto userParamDto = new UserParamDto();
		userParamDto.setCode(code);
		userParamDto.setAccessToken(accessToken);
		userParamDto.setExpireIn(expireIn);
		userParamDto.setSinaWeiboUsername(sinaWeiboUsername);
		userParamDto.setSinaWeiboUserId(uid);
		userParamDto.setRefreshToken(refreshToken);
		
		//插入数据库
		ArrayList<UserParamDto> userParamDtoList = new ArrayList<UserParamDto>();
		userParamDtoList.add(userParamDto);
		boolean result = userFacade.addUsers(userParamDtoList);
		if (result) {
			logger.info("授权成功，插入数据库成功");
		}else  {
			logger.info("授权失败，插入数据库失败");
		}

		Map<String,String> model = new HashMap<String,String>();
		HttpSession session = 	request.getSession();
		session.setAttribute("token", accessToken);
		model.put("code", code);
		return new ModelAndView("redirect:"+ "http://localhost:7070/weibo/index.html");

	}
	public String getViewName(){
		return this.viewName;
	}

	public void setViewName(String viewName) {
		this.viewName = viewName;
	}
	public UserFacade getUserFacade() {
		return userFacade;
	}
	public void setUserFacade(UserFacade userFacade) {
		this.userFacade = userFacade;
	}

}
